package com.detroitchow;

/** 
 * Lays out the Review
 * 
 * ToDo: rethink the method of getting icons, to support growing list 
 * of review sources.. new icons should append to the file system and be used later
 */
import java.util.ArrayList;

import com.detroitchow.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ArticleArrayAdapter extends ArrayAdapter<Article> {

	
	private final Activity myContext;
  private final ArrayList<Article> articles;
  private TextView txtWeb;
  private TextView txtTitle;
  private TextView txtExcerpt;
  private LinearLayout iconReviewLayout=null;
  private Drawable icon=null;
  private ImageView iconReview=null;
  
	 public ArticleArrayAdapter(Activity context, int textViewResourceId, int elementId, ArrayList<Article> articles) {
      super(context, textViewResourceId, elementId, articles);
      this.myContext = context;
      this.articles = articles;
    }
	 
	 public View getView(int index, View convertView, ViewGroup parent){
	    
	    Article article = this.articles.get(index);
	    LogUtil.log("getView", "" + article);
	    View row = convertView;
	    if(row==null){
	      LayoutInflater inflater = myContext.getLayoutInflater();
	      row = inflater.inflate( R.layout.reviewitem, parent, false);
	    }
	    
	    iconReviewLayout = (LinearLayout) row.findViewById(R.id.iconReviewLayout);
	    icon = getSourceIcon(article.source);
	    
	    iconReview = (ImageView) row.findViewById(R.id.iconReview);
	    iconReview.setImageDrawable(icon);
	    
	    txtWeb = (TextView) row.findViewById(R.id.txtWebAddress);
	    txtWeb.setText(article.articleUrl);
	    
	    txtTitle = (TextView) row.findViewById(R.id.txtTitle);
	    if(article.title == null || article.title.length() == 0) {
	    	txtTitle.setText("");
	    	txtTitle.setHeight(0);
	    }else 
	    	txtTitle.setText(article.title);
	    
	    
	    
	    
	    txtExcerpt= (TextView) row.findViewById(R.id.txtExcerpt);
	    txtExcerpt.setText(article.excerpt);
	    
	    return row;
	  }

	private Drawable getSourceIcon(String source) {
	  // TODO Auto-generated method stub
		LogUtil.log("getSourceIcon", source);
		if(source.equals(ArticleIcon.YELP))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.yelp_icon);
		else if (source.equals(ArticleIcon.CITYSEARCH) )
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.city_search_icon);
		else if (source.equals(ArticleIcon.INSIDERPAGES) )
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.insider_icon);
		else if (source.equals(ArticleIcon.JUDYS_BOOK) )
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.judys_book_icon);
		else if (source.equals(ArticleIcon.DETROITFOODIE) )
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.detroit_foodie_icon);
		else if (source.equals(ArticleIcon.BLOG) )
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.blog_icon);
		else if (source.equals(ArticleIcon.METROTIMES ))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.metrotimes_icon);
		else if (source.equals(ArticleIcon.DETNEWS))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.detnews_icon);
		else if (source.equals(ArticleIcon.FREEP))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.freep_icon);
		else if (source.equals(ArticleIcon.SERIOUSEATS))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.serious_eats_icon);
		else if (source.equals(ArticleIcon.HOUR))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.hour_icon);
		else if (source.equals(ArticleIcon.MENUISM))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.menuism_icon);
		else if (source.equals(ArticleIcon.METROMIX))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.metro_mix_icon);
		else if (source.equals(ArticleIcon.EATITDETROIT))
			return (Drawable) this.myContext.getResources().getDrawable(R.drawable.eatitdetroit);
		return null;
  }
	 
	 

}
